﻿//获取话务系统信息 sunyangwei 2020-05-14
var call_webUrl = $("#call_webUrl").val();//webUrl地址
var call_hostUrl =$("#call_hostUrl").val();//地址
var call_agentName =$("#call_agentName").val();//用户姓名
var call_agentId =$("#call_agentId").val();//坐席ID
var user_code = $("#call_userCode").val();//工号
var call_agtPwd =$("#call_agtPwd").val();//呼叫中心用户密码
var call_domain =$("#call_domain").val();
var call_satisfactionDeviceId = $("#call_satisfactionDeviceId").val();
var call_logout_off = $("#call_logout_off").val();
var call_ready_off = $("#call_ready_off").val();
var call_ready_delay = $("#call_ready_delay").val();

var call_yunhuIp = $("#call_yunhuIp").val();

var call_yunhuIp_listen = $("#call_yunhuIp_listen").val();
var phone={
	webUrl:call_webUrl,//call_webUrl,
    monitorUrl:call_hostUrl,//来虎url
    deviceId:"",//分机号
	agtId:call_agentId,//坐席号,
    userCode:user_code,//工号,
    domain: call_domain,//域
	agtPwd: call_agtPwd,					//呼叫中心用户密码
    agentName:call_agentName,
    isPause:false,//是否暂停
    isHoldCall:false,//是否保持通话
    isConsole:true,//前端console.log开关
    logoutOFF:call_logout_off,//日志输出开关,
    readyOFF:call_ready_off,//自动进线开关
    readyDelay:call_ready_delay,//自动进线延迟
    isAfterCall:0,//是否经过后处理
	groupId:"",
    satisfactionDeviceId:call_satisfactionDeviceId,
	agtStatus:1,
	callStatus:-1,
	occupy:-1,
	aliveClock:0,
	lastAliveTm:0,
	isLogin:false,
	isMSClick:false,
	isPause:false,
	tmMonitor:0,
	dialGrp:0,
	lastCall:{
		callId:0,
		ani:"",//呼叫方
        dnis:"",//被呼叫分机
        connectionState:"",//呼叫状态
        evtName:"",//事件
		groupId:"",
		ivrList:"",
		offHookTime:""
	},
	rights:{
		agtReq:true,
		dialOut:true,
		agtDisconnectCall:true,
		agtIntercept:true,
		agtJoinTalk:true,
		agtListen:true,
		isMonitor:true,
		logoutAgt:false,
		remoteCti:true
	},
	firstMX:true,
	agtMonitorObj:null,
	toolsType:'zhuanjie',
	topics:{},
	subscribeCnt:{},
	lastAllAgtInfoStr:"",
	lastAllAgtInfoCnt:0,
	webServiceUrl:"",
	tmrDur:0,
	workTime:"",
	callCnt:0,
	dialOutCnt:0,
	talkTime:0,
	totalTalkTime:0,
	tenantsId:"",
	myId:"",
	client:"BS",
	agtConfig:{},
	afterWorkStatus:0,
	userTenantsId:"",
	userOrganizerId:"",
	aliveTmCnt:0,
    isCallIn:false,//自定义，区分呼入还是呼出
    isFinishRight:false,
    isFinishGroups:false
};

$(function () {
    if(window.location.protocol == "https:"){
        console.log("协议不支持https");
        return;
    }
    if(phone.agtId==""){
        console.log("缺少参数：userCode");
        //ztoDialog('message',"登录失败：缺少参数[userCode]");
        return;
    }
    if(phone.agtPwd==""){
        console.log("缺少参数：userPwd");
        //ztoDialog('message',"登录失败：缺少参数[userPwd]");
        return;
    }
    window.top.vm.openDlg(phone.userCode);
});

//用来写调试日志（正式使用时可注释掉里面的代码）
function writeLog(msg){
    /*if(debugout!=undefined){
        debugout.log(msg);
    }*/
    var date=new Date();
    var hours=date.getHours()<10?"0"+date.getHours():date.getHours();
    var minutes=date.getMinutes()<10?"0"+date.getMinutes():date.getMinutes();
    var seconds=date.getSeconds()<10?"0"+date.getSeconds():date.getSeconds();
    var ms;
    if(date.getMilliseconds()<10){
        ms="00"+date.getMilliseconds();
    }else if(date.getMilliseconds()<100){
        ms="0"+date.getMilliseconds();
    }else{
        ms=date.getMilliseconds();
    }

    msg=hours+":"+minutes+":"+seconds+"."+ms +"    " + msg;
    if(console!=null && console!=undefined && phone.isConsole){
        console.log(msg);
    }
    if(call_logout_off == "1"){
        var url =G_CTX_PATH+'/cc/front/logout.do';
        uceAjax(url,{'message':msg},function(data){
        });
    }
}

//监听
phone.setListen=function(){
    $("#callStatus").html("监听");
};


//清空呼叫信息
phone.clearCall = function() {
    phone.isHoldCall = false;
    phone.lastCall.callId = 0;
    phone.lastCall.subId = 0;
    phone.lastCall.areaCode = "";
    phone.lastCall.ani = "";
    phone.lastCall.groupId = "";
    phone.lastCall.evtName = "";
    phone.lastCall.connectionState = "";
    if(phone.tmrDur != ""){clearInterval(phone.tmrDur);}
    //if(phone.lastCall.offHookTime=="") return;
    var objDate = new Date();
    phone.totalTalkTime += phone.talkTime;
    phone.talkTime = 0;
    try{
        setAgtCountInfo(phone.deviceId,phone.workTime,phone.groupId,$.date.dateFormat(phone.talkTime,'hh:mm:ss'),phone.callCnt+"/"+phone.dialOutCnt,$.date.dateFormat(phone.totalTalkTime,'hh:mm:ss'),phone.agentName,"");
        //parent.setAgtCountInfo(SdAgent.agtId,phone.workTime,phone.groupId,$.date.dateFormat(phone.talkTime,'hh:mm:ss'),phone.callCnt+"/"+phone.dialOutCnt,$.date.dateFormat(phone.totalTalkTime,'hh:mm:ss'));
    }catch(e){}

    phone.lastCall.offHookTime = "";
};


/*************************方法处理**********************/
//座席监控方法
phone.openMonitor = function(type) {
    /*phone.toolsType = type;
    SdAgent.getAllAgtInfo();
    parent.showAgtMonitorDlg();
    //phone.agtMonitorObj = parent.getAgtMonitorFrame().contentWindow;
    phone.agtMonitorObj = parent.getAgtMonitorFrame()?parent.getAgtMonitorFrame().contentWindow: "";
    try{phone.agtMonitorObj.showTools(type);}catch(e){}
    if(phone.tmMonitor>0) clearInterval(phone.tmMonitor);
    phone.tmMonitor = setInterval(function(){
        SdAgent.getAllAgtInfo();
        if(parent.getMonitorDlgStatus()==0) clearInterval(phone.tmMonitor);
    },5000);*/
}

//离线状态
phone.setLogOff = function(){
    if(call1 == null && call2 != null){
        call1 = call2;
        call2 = null;
    }
    if(call1 == null && call2 == null){
        currentCallId = "";
    }
    $("#callStatus").html("离线");
    phone.agtStatus=3;
    phone.clearCall();
    $(".s-tips li").hide();
    $("#banzhang,#zhuanjie,#shimang,#guanduan,#shixian,#baochi,#hujiao,#sanfang").show().removeClass("gray");
    $("#banzhang,#zhuanjie,#shimang,#guanduan,#shixian,#baochi,#hujiao,#sanfang").addClass("gray");
//	$("#banzhang,#hujiao,#zhuanjie,#shimang,#sanfang,#guanduan,#zhijian").removeClass("gray");
    $("#queueInfo").html("0人");
    ztoDialog("离线，请检查网络是否连接")
}